package com.donggua.aichebao.commonui.refresh;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import cn.drj.refreshlayout.BGANormalRefreshViewHolder;
import cn.drj.refreshlayout.BGARefreshLayout;


/**
 * Created by Administrator on 2017-11-03.
 */

public class XXPullToRefreshRecyclerView extends BGARefreshLayout  implements BGARefreshLayout.BGARefreshLayoutDelegate{
    private RefreshListener<XXPullToRefreshRecyclerView> mListener;
    private RecyclerView mRefreshableView;
    public XXPullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
       // this.setOnRefreshListener(this);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRefreshableView = new RecyclerView(context,attrs);
        mRefreshableView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRefreshableView.setLayoutParams(layoutParams);
        this.addView(mRefreshableView);
        setRefreshViewHolder(new BGANormalRefreshViewHolder(context, true));
        setDelegate(this);
    }

    public  void setOnRefreshListener(RefreshListener<XXPullToRefreshRecyclerView> listener) {
        mListener = listener;
    }

    public RecyclerView getRefreshableView() {
        return mRefreshableView;
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if(mListener != null) {
            mListener.onPullDownToRefresh(XXPullToRefreshRecyclerView.this);
        }
    }

    public void setHasPullUpFriction(boolean is){

    }
    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if(mListener != null){
            mListener.onPullUpToRefresh(XXPullToRefreshRecyclerView.this);
        }
        return false;
    }

    public void onRefreshComplete(){
        endRefreshing();
    }
}
